package org.wak.gui.sci
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.wak.gui.events.GuiEvent;
	import org.wak.inventory.InventoryItem;
	import org.wak.inventory.events.InventoryEvent;
	import org.wak.utils.Utils;
	import org.wak.utils.WakSimpleButton;


	 /**
	 * Dispatched when the inventory item button is clicked
	 * it is dispatched even if there is no selected item.  
	 * If that is the case the inventoryItem is set to null. 
	 * 
	 * @eventType org.wak.gui.events.GuiEvent.INVENTORY_ITEM_SELECTED
	 */
	[Event(name="INVENTORY_ITEM_SELECTED", type="org.wak.gui.events.GuiEvent")]
	
	/**
	 * Class to show the last selected item in the button panel of the SCI
	 * interface. 
	 * 
	 */	
	public class SciItemSelectionButton extends WakSimpleButton
	{
		// the current selected item.
		private var mSelectedItem:InventoryItem = null ;
		
		// Sprite of the item selected
		private var mItemDisplayPanel:Sprite = null ;
		
		// background image.
		
		private var mMask:Shape = null ;
		
		
		/**
		 * Constructor. 
		 * @param pUp The initial value for the WakSimpleButton up state. 
		 * @param pOver The initial value for the WakSimpleButton over state. 
		 * @param pDown The initial value for the WakSimpleButton down state. 
		 * @param pActionName an optional button name to use as an id depending on the context.
		 * 
		 */		
		public function SciItemSelectionButton(pUp:DisplayObject, pOver:DisplayObject, pDown:DisplayObject, pMaskMargin:int, pActionName:String = "")
		{
			super(pUp, pOver, pDown, pActionName);
			
			
			
			if(mUp != null)
			{
				
				mMask = new Shape();
				mMask.graphics.beginFill(0xFFFFFF);
				mMask.graphics.drawRect(0,0,mUp.width - pMaskMargin, mUp.height - pMaskMargin);
				mMask.graphics.endFill();
				mMask.x = Utils.AlignCenter( mMask.width, pUp.width);
				mMask.y = Utils.AlignCenter( mMask.height, pUp.height);
				
				//this.mask = mMask ;
			}
			this.addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage);
		}
		
		protected override function OnAddedToStage(event:Event):void
		{
			super.OnAddedToStage(event);
			this.removeEventListener(Event.ADDED_TO_STAGE, OnAddedToStage);
			
			this.addEventListener(MouseEvent.CLICK, OnClick );
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, OnRemovedFromStage);
		}
		
		protected override function OnRemovedFromStage(event:Event):void
		{
			super.OnRemovedFromStage(event);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, OnRemovedFromStage);
			
			this.removeEventListener(MouseEvent.CLICK, OnClick );
			
			this.addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage);
		}
		
		protected function OnClick(event:MouseEvent):void
		{
			
			var fEvent:Event = new GuiEvent(GuiEvent.INVENTORY_ITEM_SELECTED, null,mSelectedItem);
			this.dispatchEvent( fEvent );
		}
		
		protected override function OnMouseOver(event:MouseEvent):void
		{
			super.OnMouseOver(event);
			
			if(mItemDisplayPanel == null)
				return ;
			
			if( this.contains(mItemDisplayPanel) )
			{
				this.setChildIndex(mItemDisplayPanel, this.numChildren -1 ) ;
			}

			
		}
		
		protected override function OnMouseOut(event:MouseEvent):void
		{
			super.OnMouseOut(event);
			
			if(mItemDisplayPanel == null)
				return ;
			
			if( this.contains(mItemDisplayPanel) )
			{
				this.setChildIndex(mItemDisplayPanel, this.numChildren -1 ) ;
			}
		}
	
		
		
		/**
		 * Set the selected item in the button. 
		 * @param pInventoryItem the new selected item.
		 * 
		 */		
		public function SetItemSelected(pInventoryItem:InventoryItem):void
		{
			mSelectedItem = pInventoryItem ;
			
			if(mItemDisplayPanel != null)
			{
				if( this.contains(mItemDisplayPanel) )
				{
					this.removeChild(mItemDisplayPanel) ;
					mItemDisplayPanel.mask = null
				}
			}
			
			if(!this.contains(mMask) )
				this.addChild(mMask);
			
			mItemDisplayPanel = mSelectedItem.guiItemDisplay ;
			mItemDisplayPanel.mouseEnabled = false ;
			this.addChild(mItemDisplayPanel);
			mItemDisplayPanel.mask = mMask ;
			mItemDisplayPanel.x = Utils.AlignCenter( mItemDisplayPanel.width, mUp.width);
			mItemDisplayPanel.y = Utils.AlignCenter( mItemDisplayPanel.height, mUp.height);
		}
		
		/**
		 * Clear the selected item in the panel. 
		 * 
		 */		
		public function ClearSelectedItem():void
		{
			mSelectedItem = null ;
			
			if(mItemDisplayPanel != null)
			{
				if( this.contains(mItemDisplayPanel) )
				{
					this.removeChild(mItemDisplayPanel) ;
				}
			}
			
		}
		
		/**
		 * The current selected item.
		 * @default null 
		 * @return Reference to an InventoryItem.
		 * 
		 */		
		public function get selectedItem():InventoryItem
		{
			return mSelectedItem ;
		}
		
		public function OnItemRemoved(event:InventoryEvent):void
		{
			if( (event.inventoryItem == mSelectedItem) && (event.inventoryItem != null) )
			{
				ClearSelectedItem();
			}
		}
		
		
	}
}